package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.SystemDictionaryItemQuery;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryItemQuery systemDictionaryItemQuery) {
        PageInfo systemDictionaryItemList = systemDictionaryItemService.list(systemDictionaryItemQuery);
        List<SystemDictionary> systemDictionaries = systemDictionaryService.listAll();
        model.addAttribute("result", systemDictionaryItemList);
        model.addAttribute("systemDictionaries", systemDictionaries);
        return "systemDictionaryItem/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem) {
        if (systemDictionaryItem.getId() == null) { // 代表新增
            systemDictionaryItemService.save(systemDictionaryItem);
        } else { // 代表修改
            systemDictionaryItemService.update(systemDictionaryItem);
        }
        return new JsonResult();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        systemDictionaryItemService.delete(id);
        return new JsonResult();
    }
}
